package cn.sxt.yxl.common.util;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

public class HttpUtil
	{
		/**
		 * 模拟Get请求
		 * @param url :请求的地址
		 * @param headerMap :信封的内容
		 * @param paramsMap :信的内容
		 * @return
		 */
		public String methodGet(String urlStr, Map<String, String> headerMap, Map<String, String> paramsMap)
		{
			ConstaFinalUtil.LOGGER.info("---- methodGet ----");
			StringBuffer sb = new StringBuffer() ; 
			/* 处理Url传参 */
			if(paramsMap.size() > 0 )
			{
				/* 传参 */
				if(urlStr.indexOf("?") == -1)
				{
					urlStr += "?" ;
				}else
				{
					urlStr += "&" ; 
				}
				for (Iterator iterator = paramsMap.entrySet().iterator(); iterator.hasNext();)
				{
					Entry me = (Entry) iterator.next();
					String key = me.getKey() + "" ;
					String val = me.getValue() + "" ;
					urlStr += key + "=" + val + "&" ; 
				}
			}
			try
			{
				/* 创建一个Url */
				URL url = new URL(urlStr);
				/* 打开一个链接 */
				HttpURLConnection connection = (HttpURLConnection) url.openConnection() ;
				
				/* 放信封 */
				for (Iterator iterator = headerMap.entrySet().iterator(); iterator.hasNext();)
				{
					Entry me = (Entry) iterator.next();
					String key = me.getKey() + "" ;
					String val = me.getValue() + "" ;
					connection.setRequestProperty(key, val);
				}
				
				ConstaFinalUtil.LOGGER.info("---状态码:{}--" , connection.getResponseCode());
				/* 输入流 
				 * 只有状态是200的时候才可以通过connection.getInputStream(),
				 * */
				/*InputStream is = connection.getInputStream() ;*/
				/* 其它的只能通过
				 * connection.getErrorStream()
				 * */
				/*InputStream is = connection.getErrorStream() ;*/
				InputStream is = null ; 
				if(connection.getResponseCode() == 200)
				{
					is  = connection.getInputStream() ; 
				}else
				{
					is  = connection.getErrorStream() ; 
				}
				BufferedReader br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
				
				String line = "" ; 
				while((line = br.readLine()) != null)
				{
					/*System.out.println(line);*/
					sb.append(line);
				}
			} catch (Exception e)
			{
			}
			return sb.toString();
		}
		
		/**
		 * 模拟Post请求
		 * @param url :请求的地址
		 * @param headerMap :信封的内容
		 * @param paramsMap :信的内容
		 * @return
		 */
		public String methodPost(String urlStr, Map<String, String> headerMap, Map<String, String> paramsMap)
		{
			ConstaFinalUtil.LOGGER.info("---- methodPost ----");
			StringBuffer sb = new StringBuffer() ; 
			try
			{
				/* 创建一个Url */
				URL url = new URL(urlStr);
				/* 打开一个链接 */
				HttpURLConnection connection = (HttpURLConnection) url.openConnection() ;
				
				/* 设置一下connection */
				connection.setDoInput(true);
				connection.setDoOutput(true);
				
				/* 放信封 */
				for (Iterator iterator = headerMap.entrySet().iterator(); iterator.hasNext();)
				{
					Entry me = (Entry) iterator.next();
					String key = me.getKey() + "" ;
					String val = me.getValue() + "" ;
					connection.setRequestProperty(key, val);
				}
				/* 设置提交的方法 */
				/*connection.setRequestMethod("POST");*/
				
				/* 设置参数 */
				StringBuffer sbPara = new StringBuffer();
				for (Iterator iterator = paramsMap.entrySet().iterator(); iterator.hasNext();)
				{
					Entry me = (Entry) iterator.next();
					String key = me.getKey() + "" ;
					String val = me.getValue() + "" ;
					sbPara.append(key + "=" + val + "&"); 
				}
				System.out.println(urlStr + "--->" + sbPara);
				
				/* 输出流 */
				OutputStream os = connection.getOutputStream() ;
				BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(os,"UTF-8"));
				bw.write(sbPara.toString());
				bw.flush();
				bw.close();
				
				ConstaFinalUtil.LOGGER.info("---状态码:{}--" , connection.getResponseCode());
				/* 输入流 */
				InputStream is = null ; 
				if(connection.getResponseCode() == 200)
				{
					is  = connection.getInputStream() ; 
				}else
				{
					is  = connection.getErrorStream() ; 
				}
				BufferedReader br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
				
				String line = "" ; 
				while((line = br.readLine()) != null)
				{
					/*System.out.println(line);*/
					sb.append(line);
				}
			} catch (Exception e)
			{
			}
			return sb.toString();
		}
}
